package com.yuxl.common.auth.controller;

import com.yuxl.common.auth.domain.UserInfo;
import com.yuxl.common.auth.service.SecurityService;
import com.yuxl.common.core.annotation.Anonymous;
import com.yuxl.common.core.domain.R;
import com.yuxl.common.auth.entity.AuthRole;
import com.yuxl.common.auth.entity.AuthUser;
import com.yuxl.common.auth.service.AuthUserService;
import com.yuxl.common.core.handler.ApiException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

@Tag(name = "用户接口")
@RequestMapping("/user")
@RestController
public class AuthUserController {

    @Resource
    private AuthUserService authUserService;
    @Resource
    private SecurityService securityService;

    @Operation(summary = "初始化用户")
    @PostMapping("/initializeUser")
    public Mono<R<List<AuthUser>>> initializeUser() {
        return authUserService.initializeUser()
                .collectList()
                .map(R::ok);
    }

    @Operation(summary = "创建用户")
    @PostMapping
    public Mono<R<AuthUser>> create(@RequestBody AuthUser entity) {
        return authUserService.create(entity)
                .map(R::ok);
    }

    @Operation(summary = "根据ID查询用户")
    @GetMapping("/{id}")
    public Mono<R<AuthUser>> getById(@Parameter(description = "用户ID") @PathVariable Long id) {
        return authUserService.findById(id)
                .map(R::ok);
    }

    @Operation(summary = "更新用户")
    @PutMapping
    public Mono<R<AuthUser>> updateById(@RequestBody AuthUser entity) {
        return authUserService.updateById(entity)
                .map(R::ok);
    }

    @Operation(summary = "更新密码")
    @PutMapping("/updatePassword")
    public Mono<R<AuthUser>> updatePasswordById(@RequestBody AuthUser entity) {
        return authUserService.updatePassword(entity)
                .map(R::ok);
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/{id}")
    public Mono<R<Void>> deleteById(@Parameter(description = "用户ID") @PathVariable Long id) {
        return authUserService.deleteById(id)
                .thenReturn(R.ok());
    }

    @Operation(summary = "批量删除用户")
    @DeleteMapping
    public Mono<R<Void>> deleteByIds(@Parameter(description = "用户ID") @RequestBody List<Long> ids) {
        return authUserService.deleteByIds(ids)
                .thenReturn(R.ok());
    }

    @Operation(summary = "条件查询用户列表")
    @PostMapping("/list")
    public Mono<R<List<AuthUser>>> list(@RequestBody AuthUser entity) {
        return authUserService.list(entity)
                .collectList()
                .map(R::ok);
    }

    @Operation(summary = "统计用户数量")
    @PostMapping("/count")
    public Mono<R<Integer>> count(@RequestBody AuthUser entity) {
        return authUserService.count(entity).map(R::ok);
    }

    @Operation(summary = "分页条件查询用户")
    @PostMapping("/page/{page}/{size}")
    public Mono<R<List<AuthUser>>> page(
            @RequestBody AuthUser entity,
            @PathVariable int page,
            @PathVariable int size
    ) {
        return authUserService.page(entity, page, size)
                .collectList()
                .map(R::ok);
    }

    @Operation(summary = "查询用户的角色")
    @PostMapping("/getRoles/{id}")
    public Mono<R<List<AuthRole>>> getRolesById(
            @PathVariable long id
    ) {
        return authUserService.getRolesById(id)
                .collectList()
                .map(R::ok);
    }

    @Operation(summary = "查询当前用户")
    @GetMapping
    public Mono<R<UserInfo>> getUserInfo() {
        return securityService.getCurrentUser()
                .flatMap(jwt->authUserService.findById(jwt.id()))
                .flatMap(entity->authUserService.getUserInfo(entity))
                .map(R::ok);
    }
}
